package org.mybatis.logic.mybatis;

import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.logic.BusinnessLogicSqlDataBase;

public abstract class BusinnessLogicSqlDataBaseImpl<T> implements BusinnessLogicSqlDataBase<T> {
	
	protected SqlSessionFactory sqlMapper = null;
	protected String resource = "org/mybatis/config/configuration.xml";
	private Reader reader = null;
	protected SqlSession session = null;
	protected T mapper = null;
	
	
	public BusinnessLogicSqlDataBaseImpl() {
		try {
			reader = Resources.getResourceAsReader(resource);
			sqlMapper = new SqlSessionFactoryBuilder().build(reader);
			System.out.println(":!");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void openSession(){
		session = sqlMapper.openSession();
	}

	@Override
	public T getMapper() {		
		return mapper;
	}
	
	@Override
	public void setMapper(Class<T> c) {		
		mapper = session.getMapper( c );
	}

	@Override
	public void commit() {
		session.commit();		
	}

	@Override
	public void close() {
		session.close();
	}
}
